package com.hongpu.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.hongpu.config.SecurityContextUtil;
import com.hongpu.domain.SysRole;
import com.hongpu.dto.SysRolePageDto;
import com.hongpu.service.SysRoleMenuService;
import com.hongpu.service.SysRoleService;
import com.hongpu.mapper.SysRoleMapper;
import com.hongpu.utils.ResultDto;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author Michael zhang
* @description 针对表【sys_role(角色表)】的数据库操作Service实现
* @createDate 2025-05-15 17:26:34
*/
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
    implements SysRoleService{

    @Autowired
    SysRoleMapper baseMapper;
    @Autowired
    SysRoleMenuService sysRoleMenuService;
    // 禁用角色 超级管理员,系统管理员
    private static final String DISABLE_ROLE_NAME = "超级管理员";
    private static final String DISABLE_ROLE_NAME2 = "系统管理员";
    private static final String DISABLE_ROLE_NAME3 = "租户管理员";

    /**
     * 启用角色
     *
     * @param id
     */
    @Override
    public boolean active(Long id) {
        return baseMapper.active(id, SecurityContextUtil.getUserId());
    }

    /**
     * 批量启用角色
     *
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean activeBatch(List<Long> ids) {
        try {
            if(CollectionUtils.isNotEmpty(ids)){
                for (Long id : ids) {
                    baseMapper.active(id, SecurityContextUtil.getUserId());
                }
                return true;
            }else {
                return false;
            }
        }catch (Exception e){
            return false;
        }
    }

    /**
     * 分页查询
     *
     * @param page
     * @param sysRole
     * @return
     */
    @Override
    public IPage<SysRole> page(IPage<?> iPage, SysRolePageDto sysRole) {
        return baseMapper.page(iPage, sysRole);
    }

    /**
     * 新增角色
     *
     * @param sysRole
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultDto add(SysRole sysRole) {
       try  {
           String roleName = sysRole.getName();
           if(roleName.contains(DISABLE_ROLE_NAME) || roleName.contains(DISABLE_ROLE_NAME2) || roleName.contains(DISABLE_ROLE_NAME3)){
               return ResultDto.fail("该角色已经被占用，不可新增。");
           }
           boolean save = save(sysRole);
           if (save) {
               Long roleId = sysRole.getId();
               List<Long> menuIdList = sysRole.getMenuIdList();
               if (CollectionUtils.isNotEmpty(menuIdList)) {
                   menuIdList.forEach(menuId -> sysRoleMenuService.saveRoleIdAndMenuId(roleId, menuId));
               }
           }
           return ResultDto.ok();
       }catch (Exception e){
           return ResultDto.fail(e.getLocalizedMessage());
       }
    }

    /**
     * 修改角色
     *
     * @param sysRole
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultDto update(SysRole sysRole) {
        try {
            boolean update = updateById(sysRole);
            if (update) {
                Long roleId = sysRole.getId();
                List<Long> menuIdList = sysRole.getMenuIdList();
                if (CollectionUtils.isNotEmpty(menuIdList)) {
                    // 根据角色ID删除角色菜单关系
                    sysRoleMenuService.removeByRoleId(roleId);
                    // 删除成功, 新增角色菜单关系
                    menuIdList.forEach(menuId -> sysRoleMenuService.saveRoleIdAndMenuId(roleId, menuId));
                }
            }
            return ResultDto.ok();
        }catch (Exception e){
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }
}




